package controllers.games;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.concurrent.TimeUnit;

import javax.swing.JButton;
import javax.swing.SwingWorker;

import models.games.SchiacciaTalpe;
import models.games.SchiacciaTalpe.Verifica;
import views.games.SvegliaTalpeView;
import core.GameLauncher;
import core.GameLauncher.State;

public class SvegliaTalpeController extends KeyAdapter implements
		ActionListener {

	private SchiacciaTalpe model;
	private SvegliaTalpeView gameView;
	private GameLauncher gameLauncher;
	private boolean started = false;

	public SvegliaTalpeController(SchiacciaTalpe model,
			SvegliaTalpeView gameView, GameLauncher gameLauncher) {
		this.model = model;
		this.gameView = gameView;
		gameView.disableAll();
		this.gameLauncher = gameLauncher;
		gameView.addActionListenerToGame(this);
		gameView.addKeyListenerToGame(this);
		// showCells();

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		JButton source = (JButton) e.getSource();
		String inputText = source.getText();
		if (inputText.equals("Ricomincia")) {
			SchiacciaTalpe newModel = new SchiacciaTalpe(model.getCurrentUser());
			SvegliaTalpeView newView = new SvegliaTalpeView(
					((SchiacciaTalpe) model).getTable());
			SvegliaTalpeController newController = new SvegliaTalpeController(
					newModel, newView, gameLauncher);
			gameLauncher.setGame(newModel, newView, newController);
			gameLauncher.startTransition(State.GAME);
			model.addGameToDatabase();
			System.out.println("Ricomincia");

		} else if (inputText.equals("Menu Giochi")) {
			gameLauncher.startTransition(State.GAME_MENU);
			model.addGameToDatabase();
			System.out.println("Menu Giochi");
		} else {
			int i = Integer.parseInt("" + source.getName().charAt(0));
			int j = Integer.parseInt("" + source.getName().charAt(2));
			Verifica v = model.verify(i, j);
			if (v == Verifica.CORRETTO) {
				gameView.setScore(model.getScore());
				// source.setBackground(Color.GRAY);
				gameView.setAwake(i, j);
				gameLauncher.correctAnswer();
			}
		}
	}

	@Override
	public void keyPressed(KeyEvent e) {
		if (! started && e.getKeyCode() == KeyEvent.VK_ENTER) {
			model.update();
			gameView.enableAll();
			gameView.refresh(model.getTable());
			showAndChangeCells();
			gameView.startTimer();
			started = true;
		}
	}

	public void showAndChangeCells() {
		JButton[][] cells = gameView.getTableView();
		// disableAll(cells);
		gameView.refresh(model.getTable());
		if (gameView.goAhead()) {
			changeSchemaWorker htw = new changeSchemaWorker();
			htw.execute();
		}
	}

	class changeSchemaWorker extends SwingWorker<Void, Void> {

		@Override
		protected Void doInBackground() throws Exception {
			try {
				TimeUnit.SECONDS.sleep(2);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			return null;
		}

		@Override
		public void done() {
			model.update();
			showAndChangeCells();
		}

	}

}
